package sf.database.meta;

import sf.core.DBField;
import sf.core.DBObject;
import sf.database.dialect.DBDialect;
import sf.tools.StringUtils;

public class EntityUtils {

    public static <T extends DBObject> String getTableName(Class<T> clz) {
        TableMapping tm = MetaHolder.getMeta(clz);
        return tm.getTableName();
    }

    public static String getColumnAlias(DBField dbField, String alias) {
        String columnName = getColumnName(dbField);
        if (StringUtils.isNotBlank(alias) && StringUtils.isNotBlank(alias)) {
            return alias + "." + columnName;
        }
        return columnName;
    }

    public static ColumnMapping getColumn(DBField dbField) {
        ColumnMapping cm = MetaHolder.getColumnMappingDirect(dbField);
        if (cm == null) {
            cm = MetaHolder.getColumnMapping(dbField);
        }
        return cm;
    }

    public static String getColumnName(DBField dbField) {
        ColumnMapping cm = getColumn(dbField);
        if (cm != null) {
            return cm.getRawColumnName();
        }
        return null;
    }

    public static String c(DBField dbField) {
        return getColumnName(dbField);
    }

    public static String c(DBField dbField, DBDialect dialect) {
        String columnName = getColumnName(dbField);
        if (dialect != null && dialect.containKeyword(columnName)) {
            columnName = dialect.wrapKeyword(columnName);
        }
        return columnName;
    }
}
